<?php
namespace app\index\controller\article;

use app\common\model\index\NavModel;
use app\common\model\paths\ArticleModel;
use app\index\controller\BaseIndex;

/**
 * 文章管理
 * @package app\index\controller\article
 * @author wangqinghui
 * @DateTime 2024-09-26 17:32:38
 */
class Article extends BaseIndex
{
    protected $check_login = true;
    /**
     * 列表
     * @author wangqinghui
     * @DateTime 2024-09-23 14:20:15
     * @return void
     */
    public function index()
    {
        $this->title = lang('列表');
        // 获取导航数据
        $nav_id = (int)$this->request->get('nav_id', 0);
        $map = [
            ['status', '=', 1],// 启用
            ['id', '=', $nav_id],
        ];
        $navInfo = NavModel::mk()->where($map)->find();
        $a_type = (int)$this->request->get('type', 1);
        $this->fetch('index', ['nav' => $navInfo, 'page_size' => 10, 'a_type' => $a_type]);
    }

    /**
     * 获取列表
     * @author wangqinghui
     * @DateTime 2024-09-23 16:23:46
     * @return void
     */
    public function list()
    {
        // 搜索内容
        $key = trim($this->request->post('key', ''));
        // 分页
        $page = (int)$this->request->post('page', 1);
        $limit = (int)$this->request->post('limit', 10);
        $type = (int)$this->request->post('type', 1);
        if(empty($type) || ($type <= 0)){
            $this->error('类型参数错误！');
        }

        // 获取实践课程数据
        $map = [
            ['status',  '=', 1],// 启用
            ['type',    '=', $type],// 文章类型
        ];
        if(!empty($key)){
            $map[] = ['title', 'like', "%{$key}%"];
        }
        $total = ArticleModel::mk()->where($map)->count();
        $list = ArticleModel::mk()->where($map)->page($page,$limit)->order('sort desc,id desc')->select()->toArray();
        $this->success('获取成功', ['total' => $total, 'list' => $list]);
    }

    /**
     * 详情
     * @author wangqinghui
     * @DateTime 2024-09-24 09:23:29
     * @return void
     */
    public function detail()
    {
        $this->title = lang('详情');
        $id = (int)$this->request->get('id', 0);
        $info = ArticleModel::mk()->where(['id' => $id])->find();
        $this->fetch('detail', ['info' => $info]);
    }
}